﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinCourseControls
{
    public partial class FrmGraphics : Form
    {
        public FrmGraphics()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 控件重绘事件-针对控件的重绘
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label1_Paint(object sender, PaintEventArgs e)
        {
            //1. 接受控件的绘图对象
            Graphics g = e.Graphics;//绘制对象
        }

        /// <summary>
        /// 画线按钮-针对窗体的重绘
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLine_Click(object sender, EventArgs e)
        {
            //3.不常用-通过图片资源的方式创建一个绘图对象
            // Graphics g1 = Graphics.FromImage(Image.FromFile("Imgs/01-dy.jpg"));

            //2.创建绘图对象-给当前窗体创见绘图对象
            Graphics g = this.CreateGraphics();
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//呈现质量
            //Pen - 设置画笔颜色
            Pen pen = new Pen(Color.Red, 2);
            //g.DrawLine(pen, new Point(200, 100), new Point(400, 100));//直线-画一条直线
            //g.DrawLine(pen, new Point(400, 100), new Point(400, 300));
            //g.DrawLine(pen, new Point(400, 300), new Point(200, 300));
            //g.DrawLine(pen, new Point(200, 300), new Point(200, 100));
            //画一组直线，点集合 - 通过点集合的方式画矩形
            //g.DrawLines(pen, new Point[]
            //{
            //    new Point(200, 100),new Point(400, 100),new Point(400, 300),
            //    new Point(200, 300),new Point(200, 300),new Point(200, 100) // 要在最后放一个起始点，这样图形才能闭合
            //});

            //画曲线
            Point[] points1 =
         {
                   new Point( 400, 30),
                   new Point( 430, 70),
                   new Point( 460, 50),
                   new Point( 500, 70)
          };
            // 画曲线方法  pen 笔  points1：坐标点集合 
            // g.DrawCurve(pen, points1);
            // g.DrawCurve(pen, points1,0.0f);//折线
            //张力  》=0.0f的值    最大，越平滑- 张力折线变弧线-建议0.5-0.6
            // g.DrawCurve(pen, points1,0.6f);
            //画闭合曲线-0.0f：设置闭合曲线张力 
            g.DrawClosedCurve(pen, points1, 0.0f, System.Drawing.Drawing2D.FillMode.Winding);

            g.FillClosedCurve(new SolidBrush(Color.Yellow), points1);  // 内部填充方法
        }

        /// <summary>
        /// 画椭圆方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnElipse_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//呈现质量
            //Pen 
            Pen pen = new Pen(Color.Red, 2);
            //g.DrawEllipse(pen, new Rectangle(200, 100, 200, 100));//椭圆

            //g.DrawEllipse(pen, new Rectangle(200, 100, 200, 200));//圆
            Rectangle rect = new Rectangle(200, 100, 200, 200); // 圆
                                                                // g.DrawEllipse(pen, rect);
            g.DrawEllipse(pen, 200, 100, 200, 200);

            Rectangle rect1 = new Rectangle(200 + 1, 100 + 1, 200 - 2, 200 - 2); // 想要在填充后看到边线的方法
            g.FillEllipse(new SolidBrush(Color.Green), rect1); // 圆心填充方法
        }

        /// <summary>
        /// 画矩形方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRectangle_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//呈现质量
            //Pen 
            Pen pen = new Pen(Color.Red, 2);
            //g.DrawRectangle(pen, new Rectangle(200, 100, 200, 100));//矩形
            //g.DrawRectangle(pen, new Rectangle(200, 100, 200, 200));//正方形
            Rectangle rect = new Rectangle(200, 100, 200, 200);
            // g.DrawRectangle(pen, rect);
            //g.DrawRectangle(pen, 200, 100, 200, 200);
            Rectangle rect1 = new Rectangle(200 + 1, 100 + 1, 200 - 2, 200 - 2);
            // g.FillRectangle(new SolidBrush(Color.Green), rect1);//矩形填充

            {  // 画多边形
                Point[] points1 =
                {
                     new Point( 200, 300),
                     new Point( 230, 340),
                     new Point( 360, 280),
                     new Point( 240, 200),
                     new Point( 220, 100)
                };
                g.DrawPolygon(pen, points1);//绘制多边形-闭合区域
                g.FillPolygon(new SolidBrush(Color.Green), points1);//多边形的填充
            }

        }

        /// <summary>
        /// 扇形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPie_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//呈现质量
            //Pen 
            Pen pen = new Pen(Color.Red, 2);  // 画笔
            Rectangle rect = new Rectangle(200, 100, 200, 200);  // 矩形

            Rectangle rect1 = new Rectangle(201, 101, 198, 198); // 填充矩形
            g.FillPie(new SolidBrush(Color.Purple), rect1, 180f, 60f);//扇形的填充
            g.DrawPie(pen, rect, 180f, 60f);// rect 扇形所在椭圆的结构  180  开始角度   60 扇形的内心角
        }

        /// <summary>
        /// 图片与文字的绘制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnImgFont_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//呈现质量
            Image img = Image.FromFile("Imgs/01-mm.jpg"); // 加载图片对象 
                                                          //图片绘制
            //g.DrawImage(img, 200, 100);//以图片的实际尺寸进行绘制
            //g.DrawImage(img, new Rectangle(200, 100, 300, 200));//指定矩形大小进行绘制 缩放与变形

            // g.DrawImage(img, new Point[] {
            //        new Point( 200, 300),
            //         new Point( 400, 240),
            //         new Point( 360, 150)
            //});//以平行四边形区域来绘制图片

            //文字绘制
            Font font = new Font("微软雅黑", 18, FontStyle.Bold); // 设置文本字体
            StringFormat format = new StringFormat(); 
            format.Alignment = StringAlignment.Center; // 设置文本水平对齐方式
            format.LineAlignment = StringAlignment.Center;  // 设置文本垂直对齐方式
            Pen pen = new Pen(Color.Red, 2);  // 画笔
            //g.RotateTransform(90);
            g.DrawRectangle(pen, new Rectangle(200, 200, 400, 100)); // 绘制矩形
            g.DrawString("你好，欢迎来到朝夕教育！", font, new SolidBrush(Color.DarkGreen), new Rectangle(200, 200, 400, 100), format); // 绘制文本

        }

        /// <summary>
        /// 画圆角矩形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnYJRect_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//呈现质量
            Pen pen = new Pen(Color.Red, 2);
            int x = 200, y = 200, width = 200, height = 100, radius = 10;
            //g.DrawLine(pen, x + radius, y, x + width - radius, y);//上边线
            ////右上角圆角
            //g.DrawArc(pen, new Rectangle(x + width - 2 * radius, y, 2 * radius, 2 * radius), 270F, 90F);
            ////右边竖线
            //g.DrawLine(pen, x + width, y+radius, x + width, y+height-radius);
            // //右下角圆角
            //g.DrawArc(pen, new Rectangle(x + width - 2 * radius, y+height- 2 * radius, 2 * radius, 2 * radius),0F, 90F);
            //g.DrawLine(pen, x + radius, y+height, x + width - radius, y+height);//下边线
            // //左下角圆角
            //g.DrawArc(pen, new Rectangle(x , y + height - 2 * radius, 2 * radius, 2 * radius), 90F, 90F);
            ////左边竖线
            //g.DrawLine(pen, x , y + radius, x, y + height - radius);
            ////左上角圆角
            //g.DrawArc(pen, new Rectangle(x, y, 2 * radius, 2 * radius), 180F, 90F);

            Rectangle rect = new Rectangle(x, y, width, height);
            GraphicsPath rectRound = GetRoundedRect(rect, 10);
            g.DrawPath(pen, rectRound);
        }

        /// <summary>
        /// 返回圆角矩形路径 
        /// </summary>
        /// <param name="rect"></param>
        /// <param name="radius"></param>
        /// <returns></returns>
        private GraphicsPath GetRoundedRect(Rectangle rect, int radius)
        {
            GraphicsPath gp = new GraphicsPath();
            int x = rect.X;
            int y = rect.Y;
            int width = rect.Width;
            int height = rect.Height;
            //左上圆角
            gp.AddArc(new Rectangle(x, y, radius * 2, radius * 2), 180, 90);
            //上线
            gp.AddLine(new PointF(x + radius, y), new PointF(x + width - radius, y));
            //右上圆角
            gp.AddArc(new Rectangle(x + width - radius * 2, y, radius * 2, radius * 2), 270, 90);
            //右线
            gp.AddLine(new PointF(x + width, y + radius), new PointF(x + width, y + height - radius));
            //右下圆角
            gp.AddArc(new Rectangle(x + width - radius * 2, y + height - radius * 2, radius * 2, radius * 2), 0, 90);
            //下线
            gp.AddLine(new PointF(x + radius, y + height), new PointF(x + width - radius, y + height));
            //左下圆角
            gp.AddArc(new Rectangle(x, y + height - radius * 2, radius * 2, radius * 2), 90, 90);
            //左线
            gp.AddLine(new PointF(x, y + radius), new PointF(x, y + height - radius));

            return gp;
        }
    }
}
